package com.fallsvc.re.networkprogramming.file.input;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * @auther falls_vc
 * description:
 * @date 2025/11/3  15:41
 */
public class Demo1 {
    public static void main1(String[] args) {
        try (InputStream inputStream = new FileInputStream("dest.txt")){

            while (true) {
//                int read = inputStream.read();
//                if (read == -1) break;
//
//
//                System.out.printf("%c",read);

                byte[] bytes = new byte[1024];
                int len = inputStream.read(bytes);
                if (len == -1) break;
                System.out.println(new String(bytes, StandardCharsets.UTF_8));

            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static void main2(String[] args) throws IOException {
        try (InputStream is = new FileInputStream("dest.txt")) {
            byte[] buf = new byte[1024];
            int len;
            while (true) {
                len = is.read(buf);
                if (len == -1) {
// 代表⽂件已经全部读完
                    break;
                }
// 每次使⽤ 3 字节进⾏ utf-8 解码，得到中⽂字符
// 利⽤ String 中的构造⽅法完成
// 这个⽅法了解下即可，不是通⽤的解决办法
                System.out.println(new String(buf,StandardCharsets.UTF_8));
                for (int i = 0; i < len; i += 3) {
                    String s = new String(buf, i, 3, "UTF-8");
                    System.out.printf("%s", s);
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        try(InputStream inputStream = new FileInputStream("dest.txt")) {
            try (Scanner scanner = new Scanner(inputStream,StandardCharsets.UTF_8)){
                while (scanner.hasNext()) {
                    System.out.println(scanner.next());
                }
            }
        }
    }
}
